-- drop function if exists sm_sc.fv_mx_slice_3d_2_2d(anyarray, int, int);
create or replace function sm_sc.fv_mx_slice_3d_2_2d
(
  i_array_3d        anyarray,  
  i_dim_sliced      int     ,               -- 被切片维度
  i_slice_pos       int     default  1      -- 被切片位置序号
)
returns anyarray
as
$$
declare
  v_ret      i_array_3d%type;
  v_len_1    int   :=   array_length(i_array_3d, 1);
  v_len_2    int   :=   array_length(i_array_3d, 2);
  v_len_3    int   :=   array_length(i_array_3d, 3);
begin
  if current_setting('pg4ml._v_is_debug_check', true) = '1'
  then 
    if i_dim_sliced not between 1 and 3
    then 
      raise exception 'i_dim_sliced is out of length.';
    elsif array_ndims(i_array_3d) <> 3
    then 
      raise exception 'ndims should be 3.';
    elsif i_slice_pos not between 1 and array_length(i_array_3d, i_dim_sliced)
    then 
      raise exception 'i_slice_pos is out of length of i_array_3d.';
    end if;
  end if;

  if i_array_3d is null 
  then 
    return null;
  elsif i_dim_sliced = 1
  then
    v_ret := array_fill(nullif(i_array_3d[1][1][1], i_array_3d[1][1][1]), array[v_len_2, v_len_3]);
    v_ret[ : ][ : ] := i_array_3d[i_slice_pos : i_slice_pos][ : ][ : ];
    return v_ret ;
  elsif i_dim_sliced = 2
  then
    v_ret := array_fill(nullif(i_array_3d[1][1][1], i_array_3d[1][1][1]), array[v_len_1, v_len_3]);
    v_ret[ : ][ : ] := i_array_3d[ : ][i_slice_pos : i_slice_pos][ : ];
    return v_ret ;
  elsif i_dim_sliced = 3
  then
    v_ret := array_fill(nullif(i_array_3d[1][1][1], i_array_3d[1][1][1]), array[v_len_1, v_len_2]);
    v_ret[ : ][ : ] := i_array_3d[ : ][ : ][i_slice_pos : i_slice_pos];
    return v_ret ;
  end if;
end
$$
language plpgsql stable
parallel safe
cost 100;
-- -- set search_path to sm_sc;
-- select 
-- sm_sc.fv_mx_slice_3d_2_2d
-- (
--   (
--     array
--     [
--       [
--         [1, 2, 3, 4, 5]     ,
--         [11, 12, 13, 14, 15],
--         [21, 22, 23, 24, 25], 
--         [31, 32, 33, 34, 35] 
--       ],
--       [
--         [41, 42, 43, 44, 45]     ,
--         [51, 52, 53, 54, 55],
--         [61, 62, 63, 64, 65], 
--         [71, 72, 73, 74, 75] 
--       ],
--       [
--         [81, 82, 83, 84, 85]     ,
--         [91, 92, 93, 94, 95],
--         [101, 102, 103, 104, 105], 
--         [111, 112, 113, 114, 115] 
--       ]
--     ]
--   )
--   , 
-- 1
-- )